[C++] Multiple inclusion in multiple files

Posted by Amumu on Stack Overflow See other posts from Stack Overflow or by Amumu
Published on 2011-02-24T15:23:14Z Indexed on 2011/02/24 15:25 UTC
Read the original article Hit count: 277

Filed under:
|

Hi everyone,

I am making a small game.

In BattleRecord.h:

#ifndef _CHARACTER_H_
#define _CHARACTER_H_
#include "Character.h"
#endif

class BattleRecord
{
public:
    Character Attacker;
    Character Defender;
    Status status;
    int DamageDealt;    
    int GoldEarned;
    int ExpGained;
};

In Character.h:

#ifndef _EQUIPMENT_H_
#define _EQUIPMENT_H_
#include "Equipment.h"
#endif

class BattleRecord;
class Character
{
BattleRecord AttackEnemy(Character &Enemy);
}

In BattleRecord.h:

#ifndef _CHARACTER_H_
#define _CHARACTEr_H_
#include "Character.h"
#endif

#ifndef _BATLE_RECORD_H_
#define _BATLE_RECORD_H_
#include "BattleRecord.h"
#endif

class GUI
{
public:
//GUI Methods, and two of these:
void ViewStats(Character &Player);
void Report(BattleRecord Record)
}

The problem here is, my Character.h and BattleRecord.h need to include each other, and this definitely will cause multiple redefinition problem. Therefore, I used forward declaration in Character.h by adding:

class BattleRecord;

The problem is sovled. But then, the GUI.h needs BattleRecord.h again for reporting the battle, so I have to include BattleRecord.h into the GUI.h. I also have to include the Character.h in order to pass into the ViewStat function. I got error and stuck with this up to this piont.

© Stack Overflow or respective owner

Related posts about c++

Related posts about header